home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / Archive / XPK / xpk_Source / test / testChunkyUnPack.c < prev    next >
C/C++ Source or Header  |  1998-11-15  |  4KB  |  168 lines

  1. #define NAME        "testChunkyUnPack"
  2. #define DISTRIBUTION    "(Freeware) "
  3. #define REVISION    "0"
  4.  
  5. /* Programmheader
  6.  
  7.     Name:        testChunkyUnPack
  8.     Author:        SDI
  9.     Distribution:    Freeware
  10.     Description:    tests chunky unpacking using XpkRead
  11.     Compileropts:    -
  12.     Linkeropts:    -l xpkmaster amiga
  13.  
  14.  1.0   27.08.98 : first version
  15. */
  16.  
  17. /* For correct working you need to use at least xpkmaster.library 5! */
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <proto/xpkmaster.h>
  22. #include <exec/memory.h>
  23. #include <exec/execbase.h>
  24. #include "SDI_defines.h"
  25.  
  26. struct Library        *XpkBase        = 0;
  27. ULONG            DosVersion        = 37;
  28.  
  29. #define PARAM "FROM/A"
  30.  
  31. struct Args {
  32.   STRPTR from;
  33. };
  34.  
  35. struct BufData {
  36.   STRPTR       buffer;
  37.   ULONG         start;
  38.   ULONG         filled;
  39.   LONG         err;
  40.   struct XpkFib *xfib;
  41. };
  42.  
  43. LONG ReadData(struct BufData *b, APTR buf, ULONG size);
  44.  
  45. void main(void)
  46. {
  47.   struct Args args;
  48.   struct RDArgs *rda;
  49.  
  50.   if((rda = ReadArgs(PARAM, (LONG *) &args, 0)))
  51.   {
  52.     if((XpkBase = OpenLibrary(XPKNAME, 5)))
  53.     {
  54.       struct BufData b;
  55.       LONG err;
  56.  
  57.       if(!(err = XpkOpenTags(&b.xfib, XPK_InName, args.from, TAG_DONE)))
  58.       {
  59.         if((b.buffer = (STRPTR) AllocVec(b.xfib->xf_NLen, MEMF_ANY)))
  60.         {
  61.           b.start = 0;
  62.           b.filled = 0;
  63.           b.err    = 0;
  64.  
  65.       /* this is a test, loading useless stuff saved with textChunkyPack */
  66.       {
  67.         ULONG buf[50];
  68.  
  69.         /* because of delayed error, we need not test all calls! */
  70.         if((err = ReadData(&b, buf, 8)))
  71.           XpkPrintFault(err, 0);
  72.         else if(buf[0] == 0x49445854 && buf[1] == 0x43503130)
  73.         {
  74.           Printf("ID value IDXTCP10 is correct\n");
  75.  
  76.           if((err = ReadData(&b, buf, 21)))
  77.             XpkPrintFault(err, 0);
  78.           else
  79.           {
  80.             Printf("%s\n", buf);    /* dosbase text */
  81.             ReadData(&b, 0, sizeof(struct DosLibrary));
  82.             if((err = ReadData(&b, buf, 21)))
  83.               XpkPrintFault(err, 0);
  84.             else
  85.             {
  86.               Printf("%s\n", buf); /* sysbase text */
  87.               ReadData(&b, 0, sizeof(struct ExecBase));
  88.               if((err = ReadData(&b, buf, 21)))
  89.                 XpkPrintFault(err, 0);
  90.               else
  91.               {
  92.                 ULONG a = *((ULONG *)(((STRPTR) 0x1000000)-20));
  93.                 Printf("%s\n", buf); /* kickstart text */
  94.                 if((err = ReadData(&b, 0, a-20)))
  95.                   XpkPrintFault(err, 0);
  96.                 else
  97.                 {
  98.                   if((err = ReadData(&b, buf, 4)))
  99.                     XpkPrintFault(err, 0);
  100.                   else
  101.                     Printf("Kickstart size is correct: %ld\n", buf[0]);
  102.                   Printf("Now read too much and produce an error\n");
  103.                   if((err = ReadData(&b, 0, 10000)))
  104.                     XpkPrintFault(err, 0);
  105.                 }
  106.               }
  107.             }
  108.           }
  109.         }
  110.         else
  111.           Printf("Wrong ID\n");
  112.       }
  113.           
  114.           FreeVec(b.buffer);
  115.         }
  116.         XpkClose(b.xfib);
  117.       }
  118.       else
  119.         XpkPrintFault(err, 0);
  120.       CloseLibrary(XpkBase);
  121.     }
  122.     FreeArgs(rda);
  123.   }
  124. }
  125.  
  126. /* Call this function for reading stored data, calling it with buffer zero
  127. means skipping some data.
  128.  
  129. This function uses a delayed error report, so we need not check every call.
  130. ReadData does nothing except returning the error again, when an error
  131. occured a call before. */
  132. LONG ReadData(struct BufData *b, APTR buf, ULONG size)
  133. {
  134.   LONG i;
  135.  
  136.   while(size && !b->err)
  137.   {
  138.     if(b->filled == b->start)
  139.     {
  140.       if((i = XpkRead(b->xfib, b->buffer, b->xfib->xf_NLen)) < 0)
  141.         b->err = i;
  142.       else
  143.       {
  144.     if(!i)    /* we reached the end */
  145.       b->err = XPKERR_IOERRIN;
  146.         b->filled = i;
  147.         b->start = 0;
  148.       }
  149.     }
  150.     if(!b->err)
  151.     {
  152.       if((i = b->filled - b->start) > size)
  153.         i = size;
  154.  
  155.       if(buf)
  156.       {
  157.         CopyMem(b->buffer+b->start, buf, i);
  158.         buf = ((STRPTR) buf) + i;
  159.       }
  160.       b->start += i;
  161.       size -= i;
  162.     }
  163.   }
  164.  
  165.   return b->err;
  166. }
  167.  
  168.